package subject;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import DBUtil.DBUtil;

public class SubjectDao {

	public Subject find(int id) {
		String sql = "select * from subject where id=?";
		Subject sb = null;
		Connection conn = DBUtil.getConnection();
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			ps = conn.prepareStatement(sql);
			ps.setInt(1, id);
			rs = ps.executeQuery();
			if (rs.next()) {
				sb = new Subject();
				sb.setId(rs.getInt("id"));
				sb.setSubjectname(rs.getString("subjectname"));
				sb.setClasshour(rs.getInt("classhour"));
				sb.setGradeid(rs.getInt("gradeid"));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBUtil.close(rs, ps, conn);
		}
		return sb;
	}

	public List<Subject> sblist() {
		String sql = "select * from subject";
		List<Subject> subjectlist = new ArrayList<Subject>();
		Connection conn = DBUtil.getConnection();
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			ps = conn.prepareStatement(sql);
			rs = ps.executeQuery();
			while (rs.next()) {
				Subject sb = new Subject();
				sb.setId(rs.getInt("id"));
				sb.setSubjectname(rs.getString("subjectname"));
				sb.setClasshour(rs.getInt("classhour"));
				sb.setGradeid(rs.getInt("gradeid"));
				subjectlist.add(sb);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBUtil.close(rs, ps, conn);
		}
		return subjectlist;
	}
}